﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{

    
    private AudioSource sound;//声音组件

    public Text bestScore;//分数文本
    [SerializeField]
    private Sprite[] soundBtnSprites; //声音按钮切换图片
    public Button playBtn, soundBtn, shopBtn;//开始按钮，声音按钮,商店按钮
    public GameObject shopPannel;//商店界面
    public string gameScene;//下一个场景名字

    

    void Start()
    {
        bestScore.text = "" + GameManager.instance.hiScore;
        
        sound = GetComponent<AudioSource>();
        playBtn.GetComponent<Button>().onClick.AddListener(() => { PlayBtn(); });    //play    
        soundBtn.GetComponent<Button>().onClick.AddListener(() => { SoundBtn(); });    //sound
        shopBtn.GetComponent<Button>().onClick.AddListener(() => { ShopBtn(); });    //shop


        if (GameManager.instance.isMusicOn)
        {
            soundBtn.transform.GetChild(0).GetComponent<Image>().sprite = soundBtnSprites[0];

        }
        else
        {
            soundBtn.transform.GetChild(0).GetComponent<Image>().sprite = soundBtnSprites[1];

        }

    }

    
    //点击开始按钮
    void PlayBtn()
    {
        sound.Play();

        SceneManager.LoadScene(gameScene);

    }

    
    //点击声音按钮
    void SoundBtn()
    {
        sound.Play();

        if (GameManager.instance.isMusicOn)
        {
            soundBtn.transform.GetChild(0).GetComponent<Image>().sprite = soundBtnSprites[1];
            GameManager.instance.isMusicOn = false;
            GameManager.instance.Save();
        }
        else
        {
            soundBtn.transform.GetChild(0).GetComponent<Image>().sprite = soundBtnSprites[0];
            GameManager.instance.isMusicOn = true;
            GameManager.instance.Save();

        }
    }

    //点击商店按钮
    void ShopBtn()
    {
        shopPannel.SetActive(true);

    }




}